using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using BoOnce.GameLogic;
using BoOnce.Core.Components;
using FarseerGames.FarseerPhysicsDemos;
using FarseerGames.FarseerPhysicsDemos.DrawingSystem;

namespace BoOnce.Components
{
#if DEBUG
    class DebugComponent : DrawableEntityComponent
    {
        #region Private Attributes
        private static Game _Game;
        private static SpriteFont _Font;
        private static DebugComponent _Instance;
        private static Texture2D _Background;

        private SpriteBatch _SpriteBatch;
        private Boolean SHOW_PSW = false;
        private PhysicComponent _PhysicComponent;
        private CameraComponent _Camera;
        private InputComponent _Input;
        private PhysicsSimulatorView[] _PhysicsSimulatorView;
        private GraphicsDeviceManager _GraphicsDevice;
        FPSComponent _FpsComponent;
        #endregion

        #region Public Attributes
        public static DebugComponent Instance { get { return _Instance; } }
        public static Boolean SHOW_SOMEBODY_STATES = false;
        #endregion

        #region Constructor
        public DebugComponent(Game game, PhysicComponent pc, CameraComponent camera)
            : base(game)
        {
            _Game = game;
            _Camera = camera;
            _GraphicsDevice = (GraphicsDeviceManager)_Game.Services.GetService(typeof(GraphicsDeviceManager));
            _PhysicComponent = pc;
            _SpriteBatch = new SpriteBatch(_Game.GraphicsDevice);
            _Instance = this;
            _Font = _Game.Content.Load<SpriteFont>("Fonts/Arial8");
            _Background = DrawingHelper.CreateRectangleTexture(_Game.GraphicsDevice, 150, 80, Color.White,Color.LawnGreen);
            _Input = (InputComponent)game.Services.GetService(typeof(InputComponent));

            #region Init FPSComponent
            _FpsComponent = new FPSComponent(game);
            _FpsComponent.UpdateOrder = Priority.FPS_COMPONENT;
            _FpsComponent.DrawOrder = Priority.LAST;
            _FpsComponent.Enabled = true;
            _FpsComponent.Visible = false;
            Game.Components.Add(_FpsComponent);
            #endregion

            #region Init PSW
            Color[] colors = new Color[] { Color.Blue, Color.Red, Color.PapayaWhip, Color.Green, Color.Yellow, Color.Thistle, Color.Teal, Color.Snow, Color.Silver, Color.MediumTurquoise };
            int NbSimulator = pc.Simulators.Length;
            _PhysicsSimulatorView = new PhysicsSimulatorView[NbSimulator];
            for (int i = 0; i < NbSimulator; i++)
            {
                _PhysicsSimulatorView[i] = new PhysicsSimulatorView(_PhysicComponent.Simulators[i],_Camera);

                _PhysicsSimulatorView[i].AABBColor = colors[i];
                _PhysicsSimulatorView[i].VerticeColor = colors[i];
                _PhysicsSimulatorView[i].EdgeColor = colors[i];

                _PhysicsSimulatorView[i].EnableContactView = true;
                _PhysicsSimulatorView[i].EnableEdgeView = true;
                _PhysicsSimulatorView[i].EnableAABBView = false;
                _PhysicsSimulatorView[i].EnableCoordinateAxisView = false;
                _PhysicsSimulatorView[i].EnableGridView = false;
                _PhysicsSimulatorView[i].EnablePerformancePanelView = false;
                _PhysicsSimulatorView[i].EnablePinJointView = false;
                _PhysicsSimulatorView[i].EnableRevoluteJointView = false;
                _PhysicsSimulatorView[i].EnableSliderJointView = false;
                _PhysicsSimulatorView[i].EnableSpingView = false;
                _PhysicsSimulatorView[i].EnableVerticeView = false;

                _PhysicsSimulatorView[i].LoadContent(game.GraphicsDevice, game.Content);
            }
            #endregion
        }
        #endregion

        #region Static Functions
        public static void Print(SpriteBatch sb, Vector2 pos, String msg)
        {
            sb.DrawString(_Font, msg, pos, Color.White);
        }

        public static void Print(SpriteBatch sb, Vector2 pos, Color c, String msg)
        {
            sb.DrawString(_Font, msg, pos, c);
        }

        public static void Box(SpriteBatch sb, Vector2 pos, params String[] list)
        {
            Vector2 p = new Vector2(pos.X, pos.Y);
            sb.Draw(_Background, pos, Color.White);
            for (int i = 0; i < list.Length; i += 2)
            {
                sb.DrawString(_Font, list[i] + ":" + list[i + 1], p, Color.Black);
                p.Y += 10;
            }
        }
        #endregion

        #region DrawableGameComponent
        protected override void LoadContent()
        {
            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            if(_Input.IsNewPressDebug(Keys.F1))
                SHOW_SOMEBODY_STATES = !SHOW_SOMEBODY_STATES;

            if (_Input.IsNewPressDebug(Keys.F2))
                SHOW_PSW = !SHOW_PSW;

            if (_Input.IsNewPressDebug(Keys.F3))
                _FpsComponent.Visible = !_FpsComponent.Visible;

            if (_Input.IsNewPressDebug(Keys.F4))
            {
            }

            if (_Input.IsNewPressDebug(Keys.F10))
            {
                _GraphicsDevice.IsFullScreen = !_GraphicsDevice.IsFullScreen;
                _GraphicsDevice.ApplyChanges();
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            if (SHOW_PSW)
            {
                _SpriteBatch.Begin();
                foreach (PhysicsSimulatorView psw in _PhysicsSimulatorView)
                {
                    psw.Draw(_SpriteBatch);
                }
                _SpriteBatch.End();
            }
            base.Draw(gameTime);
        }
        #endregion
    }
#endif
}
